class BleUtil {
  BleUtil._();
  static int encodeBCD(int value) {
    return ((value ~/ 10) << 4) | (value % 10);
  }

  static int decodeBCD(int value) {
    return (value >> 4) * 10 + (value & 0x0F);
  }

  static DateTime getTimeByBCD(
    int year,
    int month,
    int day,
    int hour,
    int minute,
    int second,
  ) {
    DateTime now = DateTime.now();
    int yearBase = (now.year ~/ 100) * 100;
    return DateTime(
      yearBase + decodeBCD(year),
      decodeBCD(month),
      decodeBCD(day),
      decodeBCD(hour),
      decodeBCD(minute),
      decodeBCD(second),
    );
  }

  static DateTime defaultRingFormatDate(List<int> v) {
    return getTimeByBCD(v[3], v[4], v[5], v[6], v[7], v[8]);
  }

  static List<List<int>> chunk(List<int> data, int size) {
    return data.fold<List<List<int>>>([], (previous, element) {
      if (previous.isEmpty || previous.last.length == size) {
        previous.add([element]);
      } else {
        previous.last.add(element);
      }
      return previous;
    });
  }
}
